package com.tjakus.parser;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StringReader;
import java.util.*;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import com.tjakus.adv.Advertisement;
import com.tjakus.adv.UserData;
import com.tjakus.crypto.CryptoManager;
import com.tjakus.logging.Logging;

/**
 * This class represents XML content parser
 * 
 * @author tjakus
 * 
 */

public class XMLManager {

	/**
	 * Returns the list of user data from input XML file
	 * 
	 */
	public static List<UserData> getUserDataFromXmlFile(String fileName) {

		Logging.getLogger().info("XMLManager - getUserDataFromXmlFile [ STARTED ]");

		try {
			List<UserData> userDataList = new ArrayList<UserData>();

			// Convert XML file to string
			String xmldata = "";
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			String str;
			while ((str = in.readLine()) != null) {
				xmldata = xmldata + str;
			}
			Logging.getLogger().trace(
					"XMLManager - getUserDataFromXmlFile [ " + xmldata + " ]");
			in.close();

			// initialize SAX builder
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(new StringReader(xmldata));
			// Get root node (element) from XML file
			Element rootNode = doc.getRootElement();
			// Read user name, password, email and addIds from property file
			for (Element userElement : rootNode.getChildren()) {
				// For every child from root element get user data
				List<Advertisement> advertisements = new ArrayList<Advertisement>();
				UserData userData = new UserData();
				userData.setGroupId(Integer.parseInt(userElement
						.getChildText("groupId")));
				Logging.getLogger().debug(
						"XMLManager - getUserDataFromXmlFile - groupId [ "
								+ userData.getGroupId() + " ]");
				userData.setUserName(CryptoManager.decryptBase64(userElement
						.getChildText("username")));
				Logging.getLogger().debug(
						"XMLManager - getUserDataFromXmlFile - username [ "
								+ userData.getUserName() + " ]");
				userData.setPassword(CryptoManager.decryptBase64(userElement
						.getChildText("password")));
				Logging.getLogger().debug(
						"XMLManager - getUserDataFromXmlFile - password [ "
								+ userElement.getChildText("password") + " ]");
				userData.setEmail(userElement.getChildText("email"));
				Logging.getLogger().debug(
						"XMLManager - getUserDataFromXmlFile - email [ "
								+ userData.getEmail() + " ]");

				// For every child of advertisement
				for (Element advertisementElement : userElement
						.getChildren("advertisement")) {
					Advertisement advertisement = new Advertisement();
					advertisement.setAdvertisementCategoryId(advertisementElement
							.getChildText("categoryId"));
					Logging.getLogger().debug(
							"XMLManager - getUserDataFromXmlFile - categoryId [ "
									+ advertisement.getAdvertisementCategoryId() + " ]");
					advertisement.setAdvertisementTitle(advertisementElement
							.getChildText("title"));
					Logging.getLogger().debug(
							"XMLManager - getUserDataFromXmlFile - title [ "
									+ advertisement.getAdvertisementTitle() + " ]");
					advertisement.setAdvertisementDescription(advertisementElement
							.getChildText("description"));
					Logging.getLogger().debug(
							"XMLManager - getUserDataFromXmlFile - description [ "
									+ advertisement.getAdvertisementDescription() + " ]");
					advertisement.setAdvertisementPrice(advertisementElement
							.getChildText("price"));
					Logging.getLogger().debug(
							"XMLManager - getUserDataFromXmlFile - price [ "
									+ advertisement.getAdvertisementPrice() + " ]");
					
					Element pictureRootElement = advertisementElement.getChild("pictures");
					List<String> pictures = new ArrayList<>();
				// For every child of pictures
					for (Element pictureElement : pictureRootElement
							.getChildren()) {
						pictures.add(pictureElement.getText());
						Logging.getLogger().debug(
								"XMLManager - getUserDataFromXmlFile - picture [ "
										+ pictureElement.getText() + " ]");
					}
					advertisement.setPictures(pictures);				
					advertisements.add(advertisement);
					pictures = null;
				}
				userData.setAdvertisements(advertisements);
				userDataList.add(userData);
				userData = null;
				advertisements = null;
			}
			return userDataList;
		} catch (Exception e) {
			Logging.getLogger().error(
					"XMLManager - getUserDataFromXmlFile [ Unable to parse XML file ] ");
			Logging.getLogger().error(e);
			return null;
		}
	}
}
